﻿Imports MindFusion.Diagramming
Imports MindFusion.Drawing
'represents custom behavior that defines how FlowChart.NET reacts to users' actions with a mouse or other pointing devices

Public Class ClsCustomBehavior
    Inherits WinForms.Behaviors.DrawLinksBehavior

#Region "Constructors"
    Sub New(ByVal diagramView As MindFusion.Diagramming.WinForms.DiagramView)
        MyBase.New(diagramView)
    End Sub
#End Region
#Region "Functions"
#Region "Overrides & Overloads Functions"
    'overrided sub for setting mouse cursor
    Public Overrides Function SetMouseCursor(ByVal point As System.Drawing.PointF, ByRef startInteraction As Boolean) As MindFusion.Diagramming.CursorHint
        Dim LoOiginalCursorHint As MindFusion.Diagramming.CursorHint = MyBase.SetMouseCursor(point, startInteraction)
        Dim LoItem As DiagramItem = Diagram.GetItemAt(point, False)

        If LoItem IsNot Nothing AndAlso LoItem.GetType.Name = "ClsMMNodeForNewLink" Then
            Return CursorHint.AllowLink
        End If

        If LoOiginalCursorHint = CursorHint.DrawLink Then
            'if new link is going to be created, stop it
            Return CursorHint.Move
        Else
            Return LoOiginalCursorHint
        End If

        'Dim LoItem As DiagramItem = Nothing
        ''determine operation from cursors coordinates
        'Select Case MMGetCursorOperationFromCursorsCoordinates(point, LoItem)
        '    Case GeCursorOperation.kkSelectionRectangle
        '        Return CursorHint.Pointer
        '    Case GeCursorOperation.kkTableMove, GeCursorOperation.kkLinkMove, GeCursorOperation.kkContainerMove, GeCursorOperation.kkShapeNodeMove
        '        Return CursorHint.Move
        '    Case GeCursorOperation.kkLinkCreate
        '        Return CursorHint.AllowLink
        '    Case GeCursorOperation.kkTableCreate, GeCursorOperation.kkContainerCreate
        '        Return CursorHint.Pointer
        '    Case GeCursorOperation.kkTopLeftCornerModification
        '        Return CursorHint.DiagonalResize
        '    Case GeCursorOperation.kkTopCenterModification
        '        Return CursorHint.VerticalResize
        '    Case GeCursorOperation.kkTopRightCornerModification
        '        Return CursorHint.CounterDiagonalResize
        '    Case GeCursorOperation.kkMiddleRightModification
        '        Return CursorHint.HorizontalResize
        '    Case GeCursorOperation.kkBottomRightCornerModification
        '        Return CursorHint.DiagonalResize
        '    Case GeCursorOperation.kkBottomCenterModification
        '        Return CursorHint.VerticalResize
        '    Case GeCursorOperation.kkBottomLeftCornerModification
        '        Return CursorHint.CounterDiagonalResize
        '    Case GeCursorOperation.kkMiddleLeftModification
        '        Return CursorHint.HorizontalResize
        '    Case Else
        '        Debug.Print("CHYBA!!! - SetMouseCursor")
        '        Return Nothing
        'End Select
    End Function
    'overrided sub for starting drawing
    Public Overrides Function StartDraw(ByVal point As PointF) As InteractionState
        Dim LoOriginalInteractionState As InteractionState = MyBase.StartDraw(point)
        Dim LoItem As DiagramItem = Diagram.GetItemAt(point, False)

        If LoItem IsNot Nothing AndAlso LoItem.GetType.Name = "ClsMMNodeForNewLink" Then
            Dim LoNodeForNewLink As ClsMMNodeForNewLink = CType(LoItem, ClsMMNodeForNewLink)
            Dim LoMMLinkNode As ClsMMLinkNode = New ClsMMLinkNode(Diagram)
            LoMMLinkNode.Origin = LoNodeForNewLink.MasterGroup.MainItem
            Return New InteractionState(LoMMLinkNode, -1, Action.Create)
        End If

        If LoOriginalInteractionState.CurrentItem.GetType.Name = "ClsMMLinkNode" Then
            Dim LoMMLinkNode As ClsMMLinkNode = LoOriginalInteractionState.CurrentItem
            If LoOriginalInteractionState.AdjustmentHandle = -1 Then
                'if new link is going to be created, stop it
                Dim LoCurrentItem As ClsMMLinkNode = CType(LoOriginalInteractionState.CurrentItem, ClsMMLinkNode)
                Return New InteractionState(LoCurrentItem.Origin, 8, Action.Modify)
            Else
                'check if first or last control point are going to be moved
                If LoOriginalInteractionState.AdjustmentHandle = 0 Or LoOriginalInteractionState.AdjustmentHandle = LoMMLinkNode.ControlPoints.Count - 1 Then
                    Return New InteractionState(Diagram.Selection, -1, Action.Create)
                Else
                    Return LoOriginalInteractionState
                End If
            End If
        Else
            Return LoOriginalInteractionState
        End If
            'Dim LoItem As DiagramItem = Nothing
            'Dim LiManipulator As Integer = 0 'using to identify controlpoint of a link
            ''bug? for sure :-)
            'DiagramView.CurrentCursor = DiagramView.CurrentCursor
            ''determine operation from cursors coordinates
            'Select Case MMGetCursorOperationFromCursorsCoordinates(point, LoItem, LiManipulator)
            '    Case GeCursorOperation.kkSelectionRectangle
            'Return New InteractionState(Diagram.Selection, -1, Action.Create)
            '    Case GeCursorOperation.kkLinkCreate
            '        Dim LoMMLinkNode As ClsMMLinkNode = New ClsMMLinkNode(Diagram)
            '        LoMMLinkNode.Origin = LoItem
            '        Return New InteractionState(LoMMLinkNode, -1, Action.Create)
            '    Case GeCursorOperation.kkTableCreate
            '        Return New InteractionState(New ClsMMTableNode(Diagram), -1, Action.Create)
            '    Case GeCursorOperation.kkContainerCreate
            '        Return New InteractionState(New ClsMMContainerNode(Diagram), -1, Action.Create)
            '    Case GeCursorOperation.kkLinkMove
            '        Return New InteractionState(LoItem, LiManipulator, Action.Modify)
            '    Case GeCursorOperation.kkTableMove, GeCursorOperation.kkContainerMove, GeCursorOperation.kkShapeNodeMove
            '        Return New InteractionState(LoItem, 8, Action.Modify)
            '    Case GeCursorOperation.kkTopLeftCornerModification
            '        Return New InteractionState(LoItem, 0, Action.Modify)
            '    Case GeCursorOperation.kkTopCenterModification
            '        Return New InteractionState(LoItem, 4, Action.Modify)
            '    Case GeCursorOperation.kkTopRightCornerModification
            '        Return New InteractionState(LoItem, 1, Action.Modify)
            '    Case GeCursorOperation.kkMiddleRightModification
            '        Return New InteractionState(LoItem, 5, Action.Modify)
            '    Case GeCursorOperation.kkBottomRightCornerModification
            '        Return New InteractionState(LoItem, 2, Action.Modify)
            '    Case GeCursorOperation.kkBottomCenterModification
            '        Return New InteractionState(LoItem, 6, Action.Modify)
            '    Case GeCursorOperation.kkBottomLeftCornerModification
            '        Return New InteractionState(LoItem, 3, Action.Modify)
            '    Case GeCursorOperation.kkMiddleLeftModification
            '        Return New InteractionState(LoItem, 7, Action.Modify)
            '    Case Else
            '        Debug.Print("CHYBA!!! - StartDraw")
            '        Return Nothing
            'End Select
    End Function
    'overrides sub for creating a new link
    Protected Overrides Function CreateLink() As MindFusion.Diagramming.DiagramLink
        Return New ClsMMLinkNode(Diagram)
    End Function
#End Region
#Region "MM Functions"
    'sub for determination operation from cursors coordinates
    Private Function MMGetCursorOperationFromCursorsCoordinates(ByVal IoPointF As PointF, ByRef OoItem As DiagramItem, Optional ByRef OiManipulator As Integer = 0) As GeCursorOperation
        'Debug.Print("GetCursorOperationFromCursorsCoordinates")

        Dim LoItem As DiagramItem = Diagram.GetItemAt(IoPointF, False)
        If LoItem Is Nothing Then LoItem = Diagram.GetNodeAt(IoPointF, False)

        'If LoItem IsNot Nothing Then
        '    If LoItem.HitTestHandle(IoPointF, OiManipulator) Then
        '        'dragging a controlpoint of link
        '        Debug.Print(OiManipulator)
        '    Else
        '        'nothing
        '        Debug.Print("Nothing")
        '    End If
        'End If


        If LoItem IsNot Nothing Then
            OoItem = LoItem
            Select Case LoItem.GetType.Name
                Case "ShapeNode"
                    Dim LoHandleNode As ShapeNode = CType(LoItem, ShapeNode)
                    Dim ParentNode As ClsMMTableNode = CType(LoHandleNode.MasterGroup.MainItem, ClsMMTableNode)
                    OoItem = ParentNode

                    Select Case CStr(LoHandleNode.Tag)
                        Case "TOP"
                            Return GeCursorOperation.kkTopCenterModification
                        Case "RIGHT"
                            Return GeCursorOperation.kkMiddleRightModification
                        Case "BOTTOM"
                            Return GeCursorOperation.kkBottomCenterModification
                        Case "LEFT"
                            Return GeCursorOperation.kkMiddleLeftModification
                    End Select
                Case "ClsMMContainerNode"
                    Dim LoMMContainerNode As ClsMMContainerNode = CType(LoItem, ClsMMContainerNode)
                    If LoMMContainerNode.Folded Then
                        Return GeCursorOperation.kkContainerMove
                    Else
                        Select Case GetEdgeForResize(IoPointF.X, IoPointF.Y, LoMMContainerNode.Bounds, 2)
                            Case GeEdgeForResize.kkTopLeftCorner
                                Return GeCursorOperation.kkTopLeftCornerModification
                            Case GeEdgeForResize.kkTop
                                Return GeCursorOperation.kkTopCenterModification
                            Case GeEdgeForResize.kkTopRightCorner
                                Return GeCursorOperation.kkTopRightCornerModification
                            Case GeEdgeForResize.kkRight
                                Return GeCursorOperation.kkMiddleRightModification
                            Case GeEdgeForResize.kkBottomRightCorner
                                Return GeCursorOperation.kkBottomRightCornerModification
                            Case GeEdgeForResize.kkBottom
                                Return GeCursorOperation.kkBottomCenterModification
                            Case GeEdgeForResize.kkBottomLeftCorner
                                Return GeCursorOperation.kkBottomLeftCornerModification
                            Case GeEdgeForResize.kkLeft
                                Return GeCursorOperation.kkMiddleLeftModification
                            Case GeEdgeForResize.kkNoEdge
                                Return GeCursorOperation.kkContainerMove
                        End Select
                    End If
                Case "ClsMMTableNode"
                    'Dim LoMMTableNode As ClsMMTableNode = CType(LoItem, ClsMMTableNode)
                    'Select Case GetEdgeForResize(IoPointF.X, IoPointF.Y, LoMMTableNode.Bounds, 2)
                    '    Case GeEdgeForResize.kkTopLeftCorner
                    '        Return GeCursorOperation.kkTopLeftCornerModification
                    '    Case GeEdgeForResize.kkTop
                    '        Return GeCursorOperation.kkTopCenterModification
                    '    Case GeEdgeForResize.kkTopRightCorner
                    '        Return GeCursorOperation.kkTopRightCornerModification
                    '    Case GeEdgeForResize.kkRight
                    '        Return GeCursorOperation.kkMiddleRightModification
                    '    Case GeEdgeForResize.kkBottomRightCorner
                    '        Return GeCursorOperation.kkBottomRightCornerModification
                    '    Case GeEdgeForResize.kkBottom
                    '        Return GeCursorOperation.kkBottomCenterModification
                    '    Case GeEdgeForResize.kkBottomLeftCorner
                    '        Return GeCursorOperation.kkBottomLeftCornerModification
                    '    Case GeEdgeForResize.kkLeft
                    '        Return GeCursorOperation.kkMiddleLeftModification
                    '    Case GeEdgeForResize.kkNoEdge
                    '        If LoMMTableNode.MMRectangleForNewLink.Contains(IoPointF) Then
                    '            Return GeCursorOperation.kkLinkCreate
                    '        Else
                    '            Return GeCursorOperation.kkTableMove
                    '        End If
                    'End Select
                Case "ClsMMNoteNode"
                    Dim LoMMNoteNode As ClsMMNoteNode = CType(LoItem, ClsMMNoteNode)
                    Select Case GetEdgeForResize(IoPointF.X, IoPointF.Y, LoMMNoteNode.Bounds, 2)
                        Case GeEdgeForResize.kkTopLeftCorner
                            Return GeCursorOperation.kkTopLeftCornerModification
                        Case GeEdgeForResize.kkTop
                            Return GeCursorOperation.kkTopCenterModification
                        Case GeEdgeForResize.kkTopRightCorner
                            Return GeCursorOperation.kkTopRightCornerModification
                        Case GeEdgeForResize.kkRight
                            Return GeCursorOperation.kkMiddleRightModification
                        Case GeEdgeForResize.kkBottomRightCorner
                            Return GeCursorOperation.kkBottomRightCornerModification
                        Case GeEdgeForResize.kkBottom
                            Return GeCursorOperation.kkBottomCenterModification
                        Case GeEdgeForResize.kkBottomLeftCorner
                            Return GeCursorOperation.kkBottomLeftCornerModification
                        Case GeEdgeForResize.kkLeft
                            Return GeCursorOperation.kkMiddleLeftModification
                        Case GeEdgeForResize.kkNoEdge
                            If LoMMNoteNode.MMRectangleForNewLink.Contains(IoPointF) Then
                                Return GeCursorOperation.kkLinkCreate
                            Else
                                Return GeCursorOperation.kkShapeNodeMove
                            End If
                    End Select
                Case "ClsMMModuleNode"
                    Dim LoMMModuleNode As ClsMMModuleNode = CType(LoItem, ClsMMModuleNode)
                    Select Case GetEdgeForResize(IoPointF.X, IoPointF.Y, LoMMModuleNode.Bounds, 2)
                        Case GeEdgeForResize.kkTopLeftCorner
                            Return GeCursorOperation.kkTopLeftCornerModification
                        Case GeEdgeForResize.kkTop
                            Return GeCursorOperation.kkTopCenterModification
                        Case GeEdgeForResize.kkTopRightCorner
                            Return GeCursorOperation.kkTopRightCornerModification
                        Case GeEdgeForResize.kkRight
                            Return GeCursorOperation.kkMiddleRightModification
                        Case GeEdgeForResize.kkBottomRightCorner
                            Return GeCursorOperation.kkBottomRightCornerModification
                        Case GeEdgeForResize.kkBottom
                            Return GeCursorOperation.kkBottomCenterModification
                        Case GeEdgeForResize.kkBottomLeftCorner
                            Return GeCursorOperation.kkBottomLeftCornerModification
                        Case GeEdgeForResize.kkLeft
                            Return GeCursorOperation.kkMiddleLeftModification
                        Case GeEdgeForResize.kkNoEdge
                            Return GeCursorOperation.kkShapeNodeMove
                    End Select
                Case "ClsMMActorNode"
                    Dim LoMMActorNode As ClsMMActorNode = CType(LoItem, ClsMMActorNode)
                    Select Case GetEdgeForResize(IoPointF.X, IoPointF.Y, LoMMActorNode.Bounds, 2)
                        Case GeEdgeForResize.kkTopLeftCorner
                            Return GeCursorOperation.kkTopLeftCornerModification
                        Case GeEdgeForResize.kkTop
                            Return GeCursorOperation.kkTopCenterModification
                        Case GeEdgeForResize.kkTopRightCorner
                            Return GeCursorOperation.kkTopRightCornerModification
                        Case GeEdgeForResize.kkRight
                            Return GeCursorOperation.kkMiddleRightModification
                        Case GeEdgeForResize.kkBottomRightCorner
                            Return GeCursorOperation.kkBottomRightCornerModification
                        Case GeEdgeForResize.kkBottom
                            Return GeCursorOperation.kkBottomCenterModification
                        Case GeEdgeForResize.kkBottomLeftCorner
                            Return GeCursorOperation.kkBottomLeftCornerModification
                        Case GeEdgeForResize.kkLeft
                            Return GeCursorOperation.kkMiddleLeftModification
                        Case GeEdgeForResize.kkNoEdge
                            'If LoMMActorNode.MMRectangleForNewLink.Contains(IoPointF) Then
                            '    Return GeCursorOperation.kkLinkCreate
                            'Else
                            '    Return GeCursorOperation.kkShapeNodeMove
                            'End If
                    End Select
                Case "ClsMMActivityNode"
                    Dim LoMMActivityNode As ClsMMActivityNode = CType(LoItem, ClsMMActivityNode)
                    Select Case GetEdgeForResize(IoPointF.X, IoPointF.Y, LoMMActivityNode.Bounds, 2)
                        Case GeEdgeForResize.kkTopLeftCorner
                            Return GeCursorOperation.kkTopLeftCornerModification
                        Case GeEdgeForResize.kkTop
                            Return GeCursorOperation.kkTopCenterModification
                        Case GeEdgeForResize.kkTopRightCorner
                            Return GeCursorOperation.kkTopRightCornerModification
                        Case GeEdgeForResize.kkRight
                            Return GeCursorOperation.kkMiddleRightModification
                        Case GeEdgeForResize.kkBottomRightCorner
                            Return GeCursorOperation.kkBottomRightCornerModification
                        Case GeEdgeForResize.kkBottom
                            Return GeCursorOperation.kkBottomCenterModification
                        Case GeEdgeForResize.kkBottomLeftCorner
                        Case GeEdgeForResize.kkLeft
                            Return GeCursorOperation.kkMiddleLeftModification
                        Case GeEdgeForResize.kkNoEdge
                            'If LoMMActivityNode.MMRectangleForNewLink.Contains(IoPointF) Then
                            '    Return GeCursorOperation.kkLinkCreate
                            'Else
                            '    Return GeCursorOperation.kkShapeNodeMove
                            'End If
                    End Select
                Case "ClsMMSystemBoundaryNode"
                    Dim LoMMSystemBoundaryNode As ClsMMSystemBoundaryNode = CType(LoItem, ClsMMSystemBoundaryNode)
                    If LoMMSystemBoundaryNode.Folded Then
                        Return GeCursorOperation.kkContainerMove
                    Else
                        Select Case GetEdgeForResize(IoPointF.X, IoPointF.Y, LoMMSystemBoundaryNode.Bounds, 2)
                            Case GeEdgeForResize.kkTopLeftCorner
                                Return GeCursorOperation.kkTopLeftCornerModification
                            Case GeEdgeForResize.kkTop
                                Return GeCursorOperation.kkTopCenterModification
                            Case GeEdgeForResize.kkTopRightCorner
                                Return GeCursorOperation.kkTopRightCornerModification
                            Case GeEdgeForResize.kkRight
                                Return GeCursorOperation.kkMiddleRightModification
                            Case GeEdgeForResize.kkBottomRightCorner
                                Return GeCursorOperation.kkBottomRightCornerModification
                            Case GeEdgeForResize.kkBottom
                                Return GeCursorOperation.kkBottomCenterModification
                            Case GeEdgeForResize.kkBottomLeftCorner
                                Return GeCursorOperation.kkBottomLeftCornerModification
                            Case GeEdgeForResize.kkLeft
                                Return GeCursorOperation.kkMiddleLeftModification
                            Case GeEdgeForResize.kkNoEdge
                                Return GeCursorOperation.kkContainerMove
                        End Select
                    End If
                Case "ClsMMLinkNode"
                    If LoItem.HitTestHandle(IoPointF, OiManipulator) Then
                        'dragging a controlpoint of link
                        Return GeCursorOperation.kkLinkMove
                    Else
                        'nothing
                        Return GeCursorOperation.kkSelectionRectangle
                    End If
                Case Else
                    Debug.Print("CHYBA!!! - GetCursorOperationFromCursorsCoordinates")
            End Select
        Else
            Return GeCursorOperation.kkSelectionRectangle
        End If
    End Function
#End Region
#End Region

End Class
